package com.ruoyi.crm.moulds.mq;

import com.ruoyi.crm.domain.Contract;
import com.ruoyi.crm.moulds.email.CrmMailService;
import com.ruoyi.crm.moulds.sms.CrmSmsProperties;
import com.ruoyi.crm.moulds.sms.CrmSmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Component
@Slf4j
public class CrmMqConsumer {
    @Resource
    private CrmMailService crmMailService;
        @Resource
        private CrmSmsService crmSmsService;

    //接收消息 监听队列
    @RabbitListener(queues = {CrmRabbitConfig.CRM_QUEUE_CONTRACT_CRETED})
    public void consumerContractCreated(Contract contract){
        //发送邮件
        if(StringUtils.hasText(contract.getEmail())){
            crmMailService.sendContractCreatedMail(contract);
        }
        if(StringUtils.hasText(contract.getPhone())){
                     try {
                crmSmsService.sendMessage(contract.getPhone(),"短信内容","1",new String[]{contract.getName(),contract.getContractOrder()+""});
            }catch (Throwable throwable){
                log.error(throwable.getMessage(),throwable);
            }

        }
    }
}
